﻿using ACC.Business.Stn;
using ACC.Business;
using ACC.Data;
using ACC.Data.Comm;
using ACC.Data.Line;
using ACC.Device;
using System;
using ACC.Business.Process;
using System.Text;
using ACC.Device.PLC.Siemens;
using ACC.Data.ACCLog;
using ACC.LineDevice.KukaPack;
using System.Collections.Generic;
using ACC.Data.Entities.Mes;
using System.Data;
using ACC.Util.Convert;

namespace Script
{
    /// <summary>
    /// 预埋钢扎带， 人工客户端
    /// OP L1M03060 STATION L1M13061
    /// </summary>
    class L1M03060 : StnProcessKukaPack //StnProcessKukaClientWithPLC
    {
        public L1M03060(StnRunningData data)
            : base(data)
        {
            IsPalletRelationSN = false;
            IsACCReport = false;
        }

        protected override ACCDevice CreateDevice()
        {
            ACCDevice dev = base.CreateDevice();
            if (kukaSiemensDev != null)
            {
                kukaSiemensDev.DB190 = 800;
                kukaSiemensDev.DB190_Offset = 0;
                kukaSiemensDev.DB192 = 80;
                kukaSiemensDev.DB192_PLC_Offset = 66;
                kukaSiemensDev.DB193 = 800;
                kukaSiemensDev.DB193_PLC_Offset = 34;
                kukaSiemensDev.DB193_ACC_Offset = 68;
                kukaSiemensDev.DB194 = 800;
                kukaSiemensDev.DB194_PLC_Offset = 34;
                kukaSiemensDev.DB194_PLC_Data_Offset = 78;

                kukaSiemensDev.Material_COMM_Offset = 292;       // 上下料信号位偏移量
            }

            return dev;

        }

        public string CreateVirtualBarcode()
        {
            logger.Debug("Begin Create GZD Barcode");
            string barcode = PartTypeTagProcess.GetPartTypeTagValue(_data.PartNo.BomCfg, "SteelBanding", logger);
            barcode = barcode.Replace("-", "");
            return barcode;
        }

        public override ACCCode ReadLoadParam(ACCDevice dev, out ACCDevice.LoadParam param)
        {
            _data.ProcessUnitIDWhenLoad = true;
            ACCCode rs = base.ReadLoadParam(dev, out param);
            if (rs == ACCCode.ACCOK)
            {
                param.UnitSN = CreateVirtualBarcode();
                param.PartTypeID = _data.PartNo == null ? 0 : (int)_data.PartNo.BomCfg.PartTypeID;
                param.PartNo = _data.PartNo;
            }

            return rs;
        }

        public override ACCCode ReadUnloadParam(ACCDevice dev, out ACCDevice.UnloadParam param)
        {
            ACCCode rs = base.ReadUnloadParam(dev, out param);

            // TO-DO: 获取Load时生成虚拟码, must change
            param.UnitSN = _data.LastLoadUnit == null ? string.Empty : _data.LastLoadUnit.UnitSN;

            return rs;
        }
    }
}
